<template>
  <div class="app-page categories-page">
    <div class="page-header">
      <div class="header-left">
        <el-button class="back-btn" text circle @click="goBack">
          <el-icon><ArrowLeft /></el-icon>
        </el-button>
        <h2 class="app-section-title">参数分类</h2>
      </div>
      <el-button type="primary" :loading="loading" @click="loadCategories">刷新</el-button>
    </div>

    <!-- 桌面端网格布局 -->
    <div class="desktop-only">
      <el-row :gutter="16">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" v-for="item in categories" :key="item.id">
          <el-card class="category-card" shadow="hover" @click="goDefinitions(item)">
            <div class="card-title" :title="item.name">{{ item.name }}</div>
            <div class="card-desc" :title="item.description || '—'">{{ item.description || '—' }}</div>
            <div class="card-footer">
              <el-tag size="small" type="info">数量：{{ item.count ?? 0 }}</el-tag>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 移动端列表布局 -->
    <div class="mobile-only">
      <div v-if="loading" class="loading-skeleton">
        <div v-for="i in 4" :key="i" class="skeleton-item">
          <el-skeleton animated>
            <template #template>
              <div class="skeleton-header">
                <el-skeleton-item variant="circle" style="width: 48px; height: 48px;" />
                <div class="skeleton-content">
                  <el-skeleton-item variant="text" style="width: 60%;" />
                  <el-skeleton-item variant="text" style="width: 80%;" />
                </div>
                <el-skeleton-item variant="text" style="width: 30px;" />
              </div>
            </template>
          </el-skeleton>
        </div>
      </div>
      
      <div v-else>
        <div v-for="item in categories" :key="item.id" class="mobile-category-card" @click="goDefinitions(item)">
          <div class="mobile-card-header">
            <div class="category-icon-mobile">{{ getCategoryIcon(item.name) }}</div>
            <div class="category-info">
              <h4 class="category-name">{{ item.name }}</h4>
              <p class="category-desc">{{ item.description || '暂无描述' }}</p>
            </div>
            <div class="category-stats">
              <div class="stats-number">{{ item.count ?? 0 }}</div>
              <div class="stats-label">参数</div>
            </div>
          </div>
          <div class="mobile-card-footer">
            <span class="last-update">最近更新：今天</span>
            <el-icon class="arrow-icon"><ArrowRight /></el-icon>
          </div>
        </div>
      </div>
    </div>

    <el-empty v-if="!loading && !categories.length" description="暂无分类" />
  </div>
  
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getCategories } from '@/assets/api/mobile'
import { ArrowLeft, ArrowRight } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(false)
const categories = ref([])

const normalizeList = (res) => {
  if (Array.isArray(res)) return res
  if (Array.isArray(res?.data?.data)) return res.data.data
  if (Array.isArray(res?.data)) return res.data
  return []
}

const loadCategories = async () => {
  loading.value = true
  try {
    const res = await getCategories()
    categories.value = normalizeList(res)
  } catch (e) {
    ElMessage.error('获取分类失败')
  } finally {
    loading.value = false
  }
}

const goDefinitions = (category) => {
  router.push({
    path: '/param-engine/definitions',
    query: { categoryId: category?.id }
  })
}

const goBack = () => {
  router.push('/param-engine/dashboard')
}

// 获取分类图标
const getCategoryIcon = (name) => {
  const iconMap = {
    '智能引擎': '🤖',
    '数据库与连接池': '🗄️', 
    '同比阈值': '📊',
    '结果缓存': '⚡',
    '系统配置': '⚙️',
    '缓存配置': '💾',
    '监控配置': '📈'
  }
  return iconMap[name] || '📁'
}

onMounted(loadCategories)
</script>

<style scoped>
/* Categories页面根样式 */

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.back-btn {
  padding: 6px;
}

/* 桌面端样式 */
.desktop-only { display: block; }
.mobile-only { display: none; }

.category-card {
  cursor: pointer;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  min-height: 120px;
}

.card-title {
  font-weight: 600;
  font-size: 16px;
  margin-bottom: 6px;
  line-height: 1.2;
  word-break: break-word;
}

.card-desc {
  color: var(--app-subtext);
  line-height: 1.4;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 40px;
}

.card-footer {
  margin-top: auto;
}

/* 移动端分类卡片样式 */
.mobile-category-card {
  background: white;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
}

.mobile-category-card:active {
  transform: scale(0.98);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.12);
}

.mobile-card-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.category-icon-mobile {
  font-size: 32px;
  margin-right: 12px;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  border-radius: 12px;
}

.category-info {
  flex: 1;
  min-width: 0;
}

.category-name {
  font-size: 16px;
  font-weight: 600;
  color: #262626;
  margin: 0 0 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.category-desc {
  font-size: 13px;
  color: var(--app-subtext);
  margin: 0;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.category-stats {
  text-align: center;
  margin-left: 12px;
}

.stats-number {
  font-size: 20px;
  font-weight: bold;
  color: #1890ff;
  line-height: 1;
}

.stats-label {
  font-size: 11px;
  color: #8c8c8c;
  margin-top: 2px;
}

.mobile-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 8px;
  border-top: 1px solid #f0f0f0;
}

.last-update {
  font-size: 12px;
  color: #bfbfbf;
}

.arrow-icon {
  color: #d9d9d9;
  font-size: 14px;
}

/* 移动端加载骨架屏 */
.loading-skeleton {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.skeleton-item {
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.skeleton-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.skeleton-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

@media (max-width: 768px) {
  .desktop-only { display: none; }
  .mobile-only { display: block; }
  
  .card-title {
    font-size: 15px;
  }
  .card-desc {
    -webkit-line-clamp: 3;
    line-clamp: 3;
    min-height: 48px;
  }
}
</style>






